package com.acme.bankapp.service.bank;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.List;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.exceptions.ClientExistsException;

public class BankService {

    private static final String MESSAGE_MAXIMUM_AMOUNT_TO_WITHDRAW = "Maximum amount to withdraw from all accounts:";

    /**
     * Method adds client to the bank.
     *
     * @param bank
     * @param client
     * @throws ClientExistsException if client with same name already exists in the bank
     */
    public void addClient(Bank bank, Client client) throws ClientExistsException {
        bank.addClient(client);
    }

    /**
     * Method adds Collection of clients to the bank.
     *
     * @param bank
     * @param clients
     * @throws ClientExistsException
     */
    public void addClients(Bank bank, Collection<Client> clients) {
        for (Client client : clients) {
            try {
                bank.addClient(client);
            } catch (ClientExistsException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Prints maximum amount to withdraw from each of the bank client.
     *
     * @param bank
     */
    public void printMaximumAmountToWithdraw(Bank bank) {
        List<Client> clients = bank.getClients();

        System.out.println();
        System.out.println(MESSAGE_MAXIMUM_AMOUNT_TO_WITHDRAW);
        for (Client client : clients) {
            System.out.println(client.getAccounts().maximumAmountToWithdraw());
        }
    }

    /**
     * Method serialize bank object to the file.
     *
     * @param bank than should be serialized
     * @throws IOException
     */
    public void saveBank(Bank bank, String pathTo) throws IOException {
        FileOutputStream out = null;
        ObjectOutputStream oos = null;

        try {
            out = new FileOutputStream(pathTo);
            oos = new ObjectOutputStream(out);
            oos.writeObject(bank);
        } finally {
            if (out != null) {
                out.close();
            }
            if (oos != null) {
                oos.close();
            }
        }
    }

    /**
     * Method deserialize bank object from the file.
     *
     * @param pathFrom String path to file
     * @return Bank instance or null
     * @throws IOException
     */
    public Bank readBank(String pathFrom) throws IOException {
        FileInputStream in = null;
        ObjectInputStream ois = null;
        Bank bank = null;

        try {
            in = new FileInputStream(pathFrom);
            ois = new ObjectInputStream(in);
            try {
                bank = (Bank) ois.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (ois != null) {
                ois.close();
            }
        }
        return bank;
    }

}